package com.celesea.workflow.core.v1.controller.request;

import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.vo.DictionaryVo;
import com.celesea.workflow.core.entity.WorkflowPermission;
import com.celesea.workflow.core.entity.WorkflowPermission.WorkflowPermissionType;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

/**
 * @author JackYuan
 * @create 2017-11-28 16:51
 */
public class PermissionRequest {

    @ApiModelProperty(value = "流程类型")
    private Integer workflowType;

    @ApiModelProperty(value = "流程使用授权用户")
    private List<DictionaryVo> submitUser;

    @ApiModelProperty(value = "流程使用授权角色")
    private List<DictionaryVo> submitRole;

    @ApiModelProperty(value = "流程使用授权部门")
    private List<DictionaryVo> submitDept;

    @ApiModelProperty(value = "流程使用授权群组")
    private List<DictionaryVo> submitGroup;


    @ApiModelProperty(value = "流程统计授权用户")
    private List<DictionaryVo> statisticsUser;

    @ApiModelProperty(value = "流程统计授权角色")
    private List<DictionaryVo> statisticsRole;

    @ApiModelProperty(value = "流程统计授权部门")
    private List<DictionaryVo> statisticsDept;

    @ApiModelProperty(value = "流程统计授权群组")
    private List<DictionaryVo> statisticsGroup;


    @ApiModelProperty(value = "流程统计授权用户")
    private List<DictionaryVo> defineUser;

    @ApiModelProperty(value = "流程统计授权角色")
    private List<DictionaryVo> defineRole;

    @ApiModelProperty(value = "流程统计授权部门")
    private List<DictionaryVo> defineDept;

    @ApiModelProperty(value = "流程统计授权群组")
    private List<DictionaryVo> defineGroup;

    public List<WorkflowPermission> mergePermission() {
        List<WorkflowPermission> permissions = Lists.newArrayList();

        int permissionType = WorkflowPermissionType.SUBMIT.getCode();
        merge(permissions, submitUser, permissionType, (permission, item) -> permission.setUserId(item.getId()));
        merge(permissions, submitDept, permissionType, (permission, item) -> permission.setDeptId(item.getId()));
        merge(permissions, submitRole, permissionType, (permission, item) -> permission.setRoleId(item.getId()));
        merge(permissions, submitGroup, permissionType, (permission, item) -> permission.setGroupId(item.getId()));

        permissionType = WorkflowPermissionType.STATISTICS.getCode();
        merge(permissions, statisticsUser, permissionType, (permission, item) -> permission.setUserId(item.getId()));
        merge(permissions, statisticsDept, permissionType, (permission, item) -> permission.setDeptId(item.getId()));
        merge(permissions, statisticsRole, permissionType, (permission, item) -> permission.setRoleId(item.getId()));
        merge(permissions, statisticsGroup, permissionType, (permission, item) -> permission.setGroupId(item.getId()));

        permissionType = WorkflowPermissionType.DEFINE.getCode();
        merge(permissions, defineUser, permissionType, (permission, item) -> permission.setUserId(item.getId()));
        merge(permissions, defineDept, permissionType, (permission, item) -> permission.setDeptId(item.getId()));
        merge(permissions, defineRole, permissionType, (permission, item) -> permission.setRoleId(item.getId()));
        merge(permissions, defineGroup, permissionType, (permission, item) -> permission.setGroupId(item.getId()));

        return permissions;

    }

    private void merge(
            List<WorkflowPermission> permissions,
            List<DictionaryVo> detps,
            Integer permissionType,
            IMerge merge) {
        if (CollectionKit.isEmpty(detps)) {
            return;
        }
        detps.forEach(item -> {
            WorkflowPermission permission = new WorkflowPermission();
            merge.apply(permission, item);
            permission.setName(item.getName());
            permission.setType(permissionType);
            permission.setWorkflowType(this.getWorkflowType());
            permissions.add(permission);
        });
    }

    private interface IMerge {
        void apply(WorkflowPermission permission, DictionaryVo item);
    }


    public Integer getWorkflowType() {
        return workflowType;
    }

    public void setWorkflowType(Integer workflowType) {
        this.workflowType = workflowType;
    }

    public List<DictionaryVo> getSubmitUser() {
        return submitUser;
    }

    public void setSubmitUser(List<DictionaryVo> submitUser) {
        this.submitUser = submitUser;
    }

    public List<DictionaryVo> getSubmitRole() {
        return submitRole;
    }

    public void setSubmitRole(List<DictionaryVo> submitRole) {
        this.submitRole = submitRole;
    }

    public List<DictionaryVo> getSubmitDept() {
        return submitDept;
    }

    public void setSubmitDept(List<DictionaryVo> submitDept) {
        this.submitDept = submitDept;
    }

    public List<DictionaryVo> getSubmitGroup() {
        return submitGroup;
    }

    public void setSubmitGroup(List<DictionaryVo> submitGroup) {
        this.submitGroup = submitGroup;
    }

    public List<DictionaryVo> getStatisticsUser() {
        return statisticsUser;
    }

    public void setStatisticsUser(List<DictionaryVo> statisticsUser) {
        this.statisticsUser = statisticsUser;
    }

    public List<DictionaryVo> getStatisticsRole() {
        return statisticsRole;
    }

    public void setStatisticsRole(List<DictionaryVo> statisticsRole) {
        this.statisticsRole = statisticsRole;
    }

    public List<DictionaryVo> getStatisticsDept() {
        return statisticsDept;
    }

    public void setStatisticsDept(List<DictionaryVo> statisticsDept) {
        this.statisticsDept = statisticsDept;
    }

    public List<DictionaryVo> getStatisticsGroup() {
        return statisticsGroup;
    }

    public void setStatisticsGroup(List<DictionaryVo> statisticsGroup) {
        this.statisticsGroup = statisticsGroup;
    }

    public List<DictionaryVo> getDefineUser() {
        return defineUser;
    }

    public void setDefineUser(List<DictionaryVo> defineUser) {
        this.defineUser = defineUser;
    }

    public List<DictionaryVo> getDefineRole() {
        return defineRole;
    }

    public void setDefineRole(List<DictionaryVo> defineRole) {
        this.defineRole = defineRole;
    }

    public List<DictionaryVo> getDefineDept() {
        return defineDept;
    }

    public void setDefineDept(List<DictionaryVo> defineDept) {
        this.defineDept = defineDept;
    }

    public List<DictionaryVo> getDefineGroup() {
        return defineGroup;
    }

    public void setDefineGroup(List<DictionaryVo> defineGroup) {
        this.defineGroup = defineGroup;
    }

}
